Conditions | 1 |
Paths | 1 |
Total Lines | 234 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | /** |
||
16 | function (state, format, visibility, data, util, reactionService, upgradeService) { |
||
17 | let ct = this; |
||
18 | ct.state = state; |
||
19 | ct.data = data; |
||
20 | ct.util = util; |
||
21 | ct.format = format; |
||
22 | ct.upgradeService = upgradeService; |
||
23 | ct.adjustAmount = [1, 10, 25, 100]; |
||
24 | |||
25 | function getFermiRadius(resource) { |
||
26 | let isotope = data.resources[resource]; |
||
27 | let A = isotope.energy/data.constants.U_TO_EV; |
||
28 | return data.constants.FERMI_RADIUS * Math.pow(A, 0.3333); |
||
29 | } |
||
30 | |||
31 | function getZ(resource){ |
||
32 | let isotope = data.resources[resource]; |
||
33 | let element = Object.keys(isotope.elements)[0]; |
||
34 | return data.elements[element].number; |
||
35 | } |
||
36 | |||
37 | ct.getCapacity = function(resource, player) { |
||
38 | let isotope = data.resources[resource]; |
||
39 | let element = Object.keys(isotope.elements)[0]; |
||
40 | let r = data.elements[element].van_der_waals; |
||
41 | let area = Math.PI*r*r; |
||
42 | return util.calculateValue(data.global_upgrades.fusion_area.power.base, |
||
43 | data.global_upgrades.fusion_area.power, |
||
44 | player.global_upgrades_current.fusion_area)/area; |
||
45 | }; |
||
46 | |||
47 | ct.getTime = function(player) { |
||
48 | let time = ct.getFusionReaction(player).reactant.eV/util.calculateValue(data.global_upgrades.fusion_bandwidth.power.base, |
||
49 | data.global_upgrades.fusion_bandwidth.power, |
||
50 | player.global_upgrades.fusion_bandwidth); |
||
51 | time = Math.floor(time); |
||
52 | return Math.max(1, time); |
||
53 | }; |
||
54 | |||
55 | ct.getProductIsotope = function(beam, target) { |
||
56 | let beamN = parseInt(beam, 10); |
||
57 | let targetN = parseInt(target, 10); |
||
58 | |||
59 | let beamZ = getZ(beam); |
||
60 | let targetZ = getZ(target); |
||
61 | |||
62 | let productN = beamN+targetN; |
||
63 | let productZ = beamZ+targetZ; |
||
64 | |||
65 | return data.resource_matrix[productZ][productN]; |
||
66 | }; |
||
67 | |||
68 | ct.getProductEnergy = function(beam, target) { |
||
69 | let product = ct.getProductIsotope(beam, target); |
||
70 | if(!product){ |
||
71 | return 0; |
||
72 | } |
||
73 | let beamBE = data.resources[beam].binding_energy; |
||
74 | let targetBE = data.resources[target].binding_energy; |
||
75 | let productBE = data.resources[product].binding_energy; |
||
76 | |||
77 | return productBE - (beamBE + targetBE); |
||
78 | }; |
||
79 | |||
80 | ct.getCoulombBarrier = function(beam, target) { |
||
81 | let beamZ = getZ(beam); |
||
82 | let beamR = getFermiRadius(beam); |
||
83 | |||
84 | let targetZ = getZ(target); |
||
85 | let targetR = getFermiRadius(target); |
||
86 | |||
87 | let coulombBarrier = data.constants.COULOMB_CONSTANT*beamZ*targetZ* |
||
88 | Math.pow(data.constants.ELECTRON_CHARGE, 2)/(beamR+targetR); |
||
89 | return coulombBarrier * data.constants.JOULE_TO_EV; |
||
90 | }; |
||
91 | |||
92 | ct.getYieldPercent = function(player) { |
||
93 | let beam = player.fusion[0].beam; |
||
94 | let target = player.fusion[0].target; |
||
95 | let beamR = getFermiRadius(beam.name); |
||
96 | let targetR = getFermiRadius(target.name); |
||
97 | let beamArea = Math.PI*beamR*beamR; |
||
98 | let targetArea = Math.PI*targetR*targetR; |
||
99 | |||
100 | let reactorArea = util.calculateValue(data.global_upgrades.fusion_area.power.base, |
||
101 | data.global_upgrades.fusion_area.power, |
||
102 | player.global_upgrades_current.fusion_area); |
||
103 | let beamPercentArea = beamArea*beam.number/reactorArea; |
||
104 | let targetPercentArea = targetArea*target.number/reactorArea; |
||
105 | |||
106 | return beamPercentArea*targetPercentArea; |
||
107 | }; |
||
108 | |||
109 | ct.getYield = function(player){ |
||
110 | let percentYield = ct.getYieldPercent(player); |
||
111 | let target = player.fusion[0].target.number; |
||
112 | let beam = player.fusion[0].beam.number; |
||
113 | // the yield comes from wherever source is more abundant |
||
114 | let impacted = Math.max(target, beam); |
||
115 | return Math.floor(percentYield*impacted); |
||
116 | }; |
||
117 | |||
118 | ct.getFusionReaction = function(player) { |
||
119 | let reaction = { |
||
120 | reactant: {}, |
||
121 | product: {} |
||
122 | }; |
||
123 | |||
124 | let beam = player.fusion[0].beam; |
||
125 | let target = player.fusion[0].target; |
||
126 | |||
127 | reaction.reactant[beam.name] = beam.number; |
||
128 | reaction.reactant[target.name] = target.number; |
||
129 | |||
130 | let coulombBarrier = ct.getCoulombBarrier(beam.name, target.name); |
||
131 | reaction.reactant.eV = coulombBarrier*beam.number; |
||
132 | |||
133 | let product = ct.getProductIsotope(beam.name, target.name); |
||
134 | let numberYield = ct.getYield(player); |
||
135 | |||
136 | reaction.product[product] = numberYield; |
||
137 | |||
138 | // return the leftovers from the reaction |
||
139 | if(numberYield < beam.number){ |
||
140 | reaction.product[beam.name] = beam.number - numberYield; |
||
141 | } |
||
142 | if(numberYield < target.number){ |
||
143 | reaction.product[target.name] = target.number - numberYield; |
||
144 | } |
||
145 | |||
146 | let energyExchange = ct.getProductEnergy(beam.name, target.name); |
||
147 | if(energyExchange < 0){ |
||
148 | reaction.reactant.eV += energyExchange*numberYield; |
||
149 | }else if(energyExchange > 0){ |
||
150 | reaction.product.eV = energyExchange*numberYield; |
||
151 | } |
||
152 | |||
153 | return reaction; |
||
154 | }; |
||
155 | |||
156 | function activateFusion(player){ |
||
157 | let beam = player.fusion[0].beam; |
||
158 | let target = player.fusion[0].target; |
||
159 | |||
160 | if(player.resources[beam.name] < beam.number || |
||
161 | player.resources[target.name] < target.number){ |
||
162 | player.fusion[0].running = false; |
||
163 | return; |
||
164 | } |
||
165 | player.resources[beam.name] -= beam.number; |
||
166 | player.resources[target.name] -= target.number; |
||
167 | |||
168 | player.fusion[0].running = true; |
||
169 | } |
||
170 | |||
171 | ct.stopFusion = function(player, fusion) { |
||
172 | if(fusion.running){ |
||
173 | let beam = player.fusion[0].beam; |
||
174 | let target = player.fusion[0].target; |
||
175 | |||
176 | player.resources[beam.name] += fusion.beam.number; |
||
177 | player.resources[target.name] += fusion.target.number; |
||
178 | } |
||
179 | |||
180 | fusion.eV = 0; |
||
181 | fusion.active = false; |
||
182 | fusion.running = false; |
||
183 | fusion.run = false; |
||
184 | }; |
||
185 | |||
186 | function updateFusion(player, fusion) { |
||
187 | let bandwidth = util.calculateValue(data.global_upgrades.fusion_bandwidth.power.base, |
||
188 | data.global_upgrades.fusion_bandwidth.power, |
||
189 | player.global_upgrades.fusion_bandwidth); |
||
190 | let spent = Math.min(player.resources.eV, bandwidth); |
||
191 | fusion.eV += spent; |
||
192 | player.resources.eV -= spent; |
||
193 | } |
||
194 | |||
195 | function endFusion(player, fusion, reaction) { |
||
196 | // energy is not lost! if there are leftovers, give them back to the player |
||
197 | let leftover = fusion.eV - reaction.reactant.eV; |
||
198 | reaction.product.eV = reaction.product.eV + leftover || leftover; |
||
199 | // Reaction checks that the player has the quantity necessary |
||
200 | // to react, but here eV is stored in the fusion object. By setting the cost to 0 |
||
201 | // we make sure that it always work |
||
202 | reaction.reactant = {eV:0}; |
||
203 | // To avoid double counting of resources in stats, we need to manually add resources |
||
204 | // to the player |
||
205 | let beam = fusion.beam.name; |
||
206 | let target = fusion.target.name; |
||
207 | |||
208 | player.resources[beam] += reaction.product[beam]; |
||
209 | player.resources[target] += reaction.product[target]; |
||
210 | reaction.product[beam] = 0; |
||
211 | reaction.product[target] = 0; |
||
212 | |||
213 | state.reactions.push({number: 1, reaction:reaction}); |
||
214 | |||
215 | fusion.eV = 0; |
||
216 | player.fusion[0].running = false; |
||
217 | } |
||
218 | |||
219 | function update(player){ |
||
220 | for(let fusion of player.fusion){ |
||
221 | if(!fusion.active){ |
||
222 | continue; |
||
223 | } |
||
224 | if(fusion.eV === 0 && fusion.run){ |
||
225 | activateFusion(player); |
||
226 | } |
||
227 | if(!fusion.running){ |
||
228 | continue; |
||
229 | } |
||
230 | updateFusion(player, fusion); |
||
231 | let reaction = ct.getFusionReaction(player); |
||
232 | if(fusion.eV >= reaction.reactant.eV){ |
||
233 | endFusion(player, fusion, reaction); |
||
234 | } |
||
235 | } |
||
236 | } |
||
237 | |||
238 | ct.adjustLevel = function(player, upgrade, amount){ |
||
239 | player.global_upgrades_current[upgrade] += amount; |
||
240 | // We cap it between 1 and the current max level |
||
241 | player.global_upgrades_current[upgrade] = Math.max(1, Math.min(player.global_upgrades_current[upgrade], player.global_upgrades[upgrade])); |
||
242 | }; |
||
243 | |||
244 | ct.visibleUpgrades = function() { |
||
245 | return visibility.visible(data.global_upgrades, upgradeService.filterByTag('fusion')); |
||
246 | }; |
||
247 | |||
248 | state.registerUpdate('fusion', update); |
||
249 | } |
||
250 | ]); |
||
251 |